<?php

// +----------------------------------------------------------------------
// | 文件 ApiIntervalBehavior.class.php
// +----------------------------------------------------------------------
// | 说明 接口调用间隔类
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

namespace Common\Behavior;
use Think\Behavior;
use Think\Hook;

use Api\Event\CacheEvent;

defined('THINK_PATH') or exit();

class ApiIntervalBehavior extends Behavior 
{

	public function run(&$param)
	{
		$interval = 1;  // 最低间隔时间
		$max = 5;       // 最多访问次数
		$redis = CacheEvent::instance(1);
		$key   = "IMBA.VISIT.ACTION.{$param['action']}.USER.{$param['uid']}";
		$visit = $redis->get($key);
		if ( $visit != null &&  $visit > $max ){
			if (!empty($param['fail']) && gettype($param['fail']) == 'object' ){
				return $param['fail']();
			}
		}
		$redis->multi();
			$redis->incr($key);
			$redis->expire($key, $interval);
		$redis->exec();
		
	}

}



